package love.unknown.servlets;

import love.unknown.fruit.dao.FruitDao;
import love.unknown.fruit.dao.impl.FruitDAOImpl;
import love.unknown.fruit.pojo.Fruit;
import love.unknown.myssm.myspingmvc.ViewBaseServlet;
import love.unknown.myssm.util.StringUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/index")
public class IndexServlet extends ViewBaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Integer pageNo = 1;
        String pageNoStr = req.getParameter("pageNo");
        if (StringUtil.isNotEmpty(pageNoStr)){
            pageNo = Integer.parseInt(pageNoStr);
        }
        HttpSession session = req.getSession();
        session.setAttribute("pageNo",pageNo);

        FruitDao fruitDao = new FruitDAOImpl();
        List<Fruit> fruitList = fruitDao.getFruitList(pageNo);
        //保存到session作用域
        session.setAttribute("fruitList",fruitList);

        int fruitCount = fruitDao.getFruitCount();
        int pageCount = (fruitCount+5-1)/5;
        session.setAttribute("pageCount",pageCount);
        super.processTemplate("index",req,resp);
    }
}
